/**
 * 跨域请求
 * @param url {String}
 * @param params {Object}，请求参数
 * @param callback {Function}，回调函数
 */
const jsonp = ({ url, params, callback }) => {
  const wd = window.global || window
  const { document } = wd

  // todo, change callback to backend callback
  const query = ['callback=JSONPCallback']
  Object.keys(params).forEach((k) => {
    query.push(`${k}=${encodeURIComponent(params[k])}`)
  })
  query.join('&')
  const script = document.createElement('script')
  script.src = `${url}?${query}`
  document.body.appendChild(script)
  wd.JSONPCallback = callback
}

export default jsonp
